package com.rainng.coursesystem.controller;

import com.rainng.coursesystem.model.ExamItem;
import com.rainng.coursesystem.model.FavouriteItem;
import com.rainng.coursesystem.repository.StudentRepository;
import com.rainng.coursesystem.service.ClassroomExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/exam")
public class ClassroomExamController {

    @Value("D:\\3")
    private String uploadDir;

    @Autowired
    private ClassroomExamService examservice;

    @Autowired
    private com.rainng.coursesystem.repository.StudentRepository StudentRepository;

    static class examReturn {
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        public Date examTime;

        public String roomNumber;

        public String courseName;// Optional field for extra info about the file

        // 构造函数
        examReturn(Date examTime, String roomNumber, String courseName) {
            this.examTime = examTime;
            this.roomNumber = roomNumber;
            this.courseName = courseName;
        }
    }


    @PostMapping("/arrangeexam")//教师发布考试通知
    public ResponseEntity<String> arrangeExam(
            @RequestParam String roomNumber,
            @RequestParam long courseId,
            @RequestParam String isRandom,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date examTime)
    {
        try {
            String message = examservice.TeacherArrangeExam(roomNumber,courseId,examTime,isRandom);
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to publish exam" + e.getMessage());
        }
    }

    @GetMapping("/studentlist")
    public ResponseEntity<List<examReturn>> studentListExam(@RequestParam Long studentId) {
        List<ExamItem> items = examservice.listExam(studentId);

        List<examReturn> examReturns=new ArrayList<>();;
        // 遍历 ExamItem 列表，为每个项创建一个 examReturn 对象
        for (ExamItem item : items) {
            String courseName=StudentRepository.findCourseNameById(item.getCourseId());
            examReturn examReturn = new examReturn(item.getExamTime(), item.getRoomNumber(), courseName);
            examReturns.add(examReturn);
        }
        return ResponseEntity.ok(examReturns);
    }

    @GetMapping("/studentlistbycourseid")
    public ResponseEntity<List<examReturn>> studentListExamByCourseId(@RequestParam Long studentId, @RequestParam Long courseId) {
        List<ExamItem> items = examservice.listExamByCourseId(studentId,courseId);
        String courseName=StudentRepository.findCourseNameById(courseId);
        List<examReturn> examReturns=new ArrayList<>();;
        // 遍历 ExamItem 列表，为每个项创建一个 examReturn 对象
        for (ExamItem item : items) {
            examReturn examReturn = new examReturn(item.getExamTime(), item.getRoomNumber(), courseName);
            examReturns.add(examReturn);
        }
        return ResponseEntity.ok(examReturns);
    }

    @GetMapping("/teacherviewexam")
    public ResponseEntity<List<examReturn>> teacherViewExam(@RequestParam Long courseId) {
        List<ExamItem> items = examservice.viewExam(courseId);
        String courseName=StudentRepository.findCourseNameById(courseId);
        List<examReturn> examReturns=new ArrayList<>();;
        Set<String> seen = new HashSet<>();  // 用于记录已经遇到的 ExamTime 和 roomNumber 组合

        // 遍历 ExamItem 列表，为每个项创建一个 examReturn 对象
        for (ExamItem item : items) {
            String key = item.getExamTime() + "-" + item.getRoomNumber();
            if (!seen.contains(key)) {  // 如果这个组合是第一次遇到
                seen.add(key);  // 添加到已见集合中
                examReturn examReturn = new examReturn(item.getExamTime(), item.getRoomNumber(), courseName);
                examReturns.add(examReturn);
            }
        }
        return ResponseEntity.ok(examReturns);
    }

    @DeleteMapping("/remove")
    public ResponseEntity<String> removeExam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date examTime, @RequestParam String roomNumber) {
        try {
            examservice.removeExam(examTime, roomNumber);
            return ResponseEntity.ok("File removed from courseresource successfully.");
        } catch (Exception e) {
            return ResponseEntity.status(404).body("File not found in courseresource: " + e.getMessage());
        }
    }

}
